package com.example.shopping.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.shopping.pojo.Type;
import com.example.shopping.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class TypeCon {
    @Autowired
    private TypeService typeService;

    /**
     * 添加类型
     * @param type 类型
     * @return
     */
    @PostMapping("/insType")
    @ResponseBody
    public String type(Type type){
        String type1 = type.getType();
        QueryWrapper<Type> typeQueryWrapper = new QueryWrapper<>();
        typeQueryWrapper.eq("type",type1);
        Type one = typeService.getOne(typeQueryWrapper);
        if (one!=null){
            throw new RuntimeException("该类型已存在");
        }
        typeService.save(type);
        return "ok";
    }

    @GetMapping("/typeList")
    @ResponseBody
    public List<Type> typeList(){
        List<Type> list = typeService.list();
        return list;
    }

    /**
     * 根据id删除用户
     * @param id 前端传入用户id
     */
    @GetMapping("/delTypeById/{id}")
    @ResponseBody
    public void delTypeById(@PathVariable Integer id){
        typeService.removeById(id);
    }
}
